package com.JobFind.logic.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.JobFind.DAO.IUserDAO;
import com.JobFind.DTO.LogDTO;
import com.JobFind.DTO.UserDTO;
import com.JobFind.DTO.UserSimplyDTO;
import com.JobFind.logic.IResumeLogic;
import com.JobFind.logic.IUserLogic;
import com.JobFind.logic.ILogLogic;
import com.JobFind.model.TbLog;
import com.JobFind.model.TbUser;
import com.opensymphony.xwork2.Result;

@Component
public class UserLogic implements IUserLogic {

	private IUserDAO userDAO;
    private IUserLogic userLogic;
    private ILogLogic loglogic;
    private IResumeLogic resumeLogic;
    
    
	public IResumeLogic getResumeLogic() {
		return resumeLogic;
	}
	
	@Resource
	public void setResumeLogic(IResumeLogic resumeLogic) {
		this.resumeLogic = resumeLogic;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	@Resource
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public IUserLogic getUserLogic() {
		return userLogic;
	}

	@Resource
	public void setUserLogic(IUserLogic userLogic) {
		this.userLogic = userLogic;
	}

	
	public ILogLogic getLoglogic() {
		return loglogic;
	}

	@Resource
	public void setLoglogic(ILogLogic loglogic) {
		this.loglogic = loglogic;
	}

	@Override
	@Transactional
	public Integer loginCheck(String userLoginName, String userPwd) throws Exception {
		Integer result = 0;
		try {
			List<TbUser> userlist = userDAO.loginCheck(userLoginName);
			if (userlist == null) {
				result = 0;
			} else if (userlist.size() == 0) {
				result = 0;
			} else {
				TbUser userbean = (TbUser) userlist.get(0);
				if (!userPwd.equals(userbean.getUserPwd())) {
					result = 0;
				}
				else{
					List<UserDTO> userDTOList = new ArrayList<UserDTO>();
					for(TbUser user:userlist){
						userDTOList.add(user.toDTO());
					}
					String userString = JSONArray.fromObject(userDTOList).toString();
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession loginuserSession = request.getSession();
					loginuserSession.setAttribute("loginusersession", userString);
					result = userbean.getUserId();
					LogDTO logDTO = new LogDTO();
					logDTO.setUserId(userlist.get(0).getUserId());
					logDTO.setLogIp(request.getRemoteAddr());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					logDTO.setLogTime(df.format(new Date()).toString());
					logDTO.setUserType(1);
					loglogic.addLog(logDTO.toDAO());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = -1;
		}
		return result;
	}
	
	@Override
	public String getUserSession() throws Exception{
		String result = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession loginuserSession = request.getSession();
			if(loginuserSession.getAttribute("loginusersession") == null)
				result= "";
			else
				result = loginuserSession.getAttribute("loginusersession").toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}
	@Override
	@Transactional
	public String getUserInfoById(Integer userId) throws Exception{
		String result = "";
		try {
			TbUser userlist = userDAO.getUserInfoById(userId);
			if (userlist != null) {
				UserDTO listUserDTO = new UserDTO();
				listUserDTO = userlist.toDTO();
				result = JSONArray.fromObject(listUserDTO).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer logoutCheck() throws Exception{
		Integer result = 0;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.getSession().removeAttribute("loginusersession");
			request.getSession().invalidate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer updateUserInfo(Integer userId,String homePlace,String birthday,String userNickName,String userNation) throws Exception{
		Integer result = 0;
		try {
			result = this.userDAO.updateUserInfo(userId,homePlace, birthday, userNickName, userNation);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer modifyUserPwd(Integer userId, String userPwd)
			throws Exception {
		Integer result = 0;
		try {
			result = this.userDAO.modifyUserPwd(userId, userPwd);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer addUser(UserDTO userDTO) throws Exception {
		Integer result = 0;
		try {
			List<TbUser> userlist1 = userDAO.loginCheck(userDTO.getUserLoginName());
			List<TbUser> userlist2 = userDAO.getUserInfoByNickName(userDTO.getUserNickName());
			if(userlist1.size()!=0){
				result = -1;//登录名已存在;
			}else if(userlist2.size()!=0){
				result = -2;//昵称已存在;
			}
			else{
				result = this.userDAO.addUser(userDTO.toDAO());
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getAllUncheckUser() throws Exception {
		String result = "";
		try {
			List<TbUser> users = this.userDAO.getAllUncheckUser();
			if(users==null || users.size()==0){
				result = "";
			}
			else{
				List<UserDTO> userlist = new ArrayList<UserDTO>();
				for(TbUser user :users){
					userlist.add(user.toDTO());
				}
				result = JSONArray.fromObject(userlist).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer checkUserInfo(Integer userId, Integer userStatus)
			throws Exception {
		Integer result = 0;
		try {
			result = this.userDAO.checkUserInfo(userId, userStatus);
			if(userStatus==1)
				this.resumeLogic.addResume(userId);
		} catch (Exception e) {
			e.printStackTrace();
			result=0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getSimplyUserInfoById(Integer userId) throws Exception {
		String result = "";
		try {
			TbUser userlist = userDAO.getUserInfoById(userId);
			if (userlist != null) {
				UserSimplyDTO listUserDTO = new UserSimplyDTO();
				listUserDTO = userlist.toSimplyDTO();
				result = JSONArray.fromObject(listUserDTO).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}
	
}
